--#include "data\config\NpcMiscCfg\HongNiangCfg.lua" once

-- 红娘
NpcDialog[78] = {}
local t = NpcDialog[78];
t.main = function(sysarg)
	local cfg = HongNiangCfg
	
	local strCond = string.format(cfg.conditionDesc, cfg.needLevel)
	-----------------
	local strDesc = ""
	for i = 1, #cfg.radios do
		strDesc = strDesc .. string.format("{radio;1;%d;%s}", i, cfg.radios[i].btnName)
	end
	
	strDesc = strDesc .. "{flag;0}同屏内玩家，即可随机获得发放的元宝"
	------------------
	local strBtnList = string.format(cfg.okBtn, "on_ok,%d")
	local localFun = string.format("OpenView,%s,%d", enLinkBuyItemWnd, cfg.itemId)
	strBtnList = strBtnList .. string.format(cfg.buyBtn, localFun)

	-----------------
	local pack = DataPack.allocPacket(sysarg, LogicCmd.npcDialogSys.sysID, LogicCmd.npcDialogSys.sub.sNpcDlgData)
	
	DataPack.writeUint64(pack, Actor.getNpc(sysarg))
	DataPack.writeByte(pack, NpcDialogType.FYSDNpcDlg)
	DataPack.writeString(pack, strCond)
	DataPack.writeString(pack, strDesc)
	DataPack.writeString(pack, strBtnList)
	DataPack.writeChar(pack, -1)
	DataPack.writeByte(pack, 0);
	DataPack.flush(pack)	
end

t.on_ok = function(sysarg, idx)
	local idx = tonumber(idx) -- 从 1 开始
	local mycfg = HongNiangCfg.radios[idx]
	if mycfg == nil then return end

	if idx == 1 then
		t.on_marry(sysarg, mycfg)
	elseif idx == 3 then
		t.on_divorce(sysarg, mycfg)
	elseif idx == 5 then
		t.on_lonely_hearts(sysarg, mycfg)
	elseif idx == 2 or idx == 4 or idx == 6 then
		t.on_red_package(sysarg, mycfg)
	end

end

-- 结婚
t.on_marry = function(sysarg, mycfg)	
--	local nLevel = Actor.getIntProperty(sysarg,PROP_CREATURE_LEVEL)
	if not Actor.checkActorLevel(sysarg, HongNiangCfg.needLevel, 0)then
		Actor.sendTipmsg(sysarg,string.format(Lang.GuildLang.m00091,HongNiangCfg.needLevel),ttFlyTip)
		return
	end

	if Actor.getSocialCurNum(sysarg,7) > 0 then
		Actor.sendTipmsg(sysarg, Lang.GuildLang.m00029,ttFlyTip)
		return
	end

	local teamid = Actor.getTeamId(sysarg)
	if teamid == 0 then		--没有组队
		Actor.sendTipmsg(sysarg,Lang.GuildLang.m00023,ttFlyTip)
		return
	end

	local myMoney = Actor.getMoneyCount(sysarg, mycfg.moneyType)
	if myMoney < mycfg.money then
		Actor.sendTipmsg(sysarg,string.format(Lang.ScriptTips.m00043, math.floor(mycfg.money/10000)),ttFlyTip)
		return
	end	

	local actList = LuaHelp.getTeamMemberList(sysarg)
	if actList == nil then
		return
	end

	local tCount = table.getn(actList)
	if tCount < 2 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.m00046, ttFlyTip)
		return
	elseif tCount > 2 then					--队伍人数不是2人
		Actor.sendTipmsg(sysarg,Lang.GuildLang.m00024,ttFlyTip)
		return
	end
	
	local nActorId = Actor.getIntProperty(sysarg, PROP_ENTITY_ID)
	local mySex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)

	local another = nil
	for i = 1, #actList do
		local _pActor = actList[i]

		if _pActor ~= nil then
			local nId = Actor.getIntProperty(_pActor,PROP_ENTITY_ID)
			if nId ~= nActorId then
				another = _pActor
				break
			end
		end
	end
	
	if another == nil then return end
	
	--local nLevel2 = Actor.getIntProperty(another,PROP_CREATURE_LEVEL)
	if not Actor.checkActorLevel(another, HongNiangCfg.needLevel, 0) then
		Actor.sendTipmsg(sysarg,string.format(Lang.GuildLang.m00092,HongNiangCfg.needLevel),ttFlyTip)
		return
	end
	
	if not Actor.isInSameScreen(sysarg,another) then			--有成员不再附近
		Actor.sendTipmsg(sysarg,Lang.GuildLang.m00025,ttFlyTip)
		return
	end

	local nSex = Actor.getIntProperty(another,PROP_ACTOR_SEX)	
	if nSex == mySex then		--同性别
		Actor.sendTipmsg(sysarg,Lang.GuildLang.m00026,ttFlyTip)
		return
	end
	
	if Actor.getSocialCurNum(another,7) > 0 then
		Actor.sendTipmsg(sysarg,Lang.GuildLang.m00028,ttFlyTip)
		return
	end
	
	Actor.closeNPCDialog(sysarg)
	
	Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.m00047, Actor.getName(another)), ttFlyTip)
	
	--求婚，等待对方回应
	local pack = DataPack.allocPacket(another, 41, 12)
	DataPack.writeInt(pack,nActorId)
	DataPack.writeString(pack,Actor.getName(sysarg))
	DataPack.flush(pack)
	
	-- 对方回应的处理:on_respond_merried()
end

-- 离婚
t.on_divorce = function(sysarg, mycfg)
	if Actor.getSocialCurNum(sysarg,7) <= 0 then		--没结婚
		Actor.sendTipmsg(sysarg,Lang.GuildLang.m00087,ttFlyTip)
		return
	end
	
	local nTagetId = Actor.getSocialRelationId(sysarg,7)
	local another = System.getEntityPtrByActorID(nTagetId)
	if another == nil then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0011, ttFlyTip)
		return
	end
	
	if not Actor.isInSameScreen(sysarg,another) then			--有成员不再附近
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.m00048, ttFlyTip)
		return
	end
	
	local myMoney = Actor.getMoneyCount(sysarg, mycfg.moneyType)
	if myMoney < mycfg.money then
		Actor.sendTipmsg(sysarg,string.format("{color;FFff0000;离婚需要%d万金币}", math.floor(mycfg.money/10000)),ttFlyTip)
		return
	end
	
	if Actor.changeMoney(sysarg, mycfg.moneyType, -mycfg.money, LogIds.DivorceConsume, "divorce") == false then
		if mycfg.moneyType == mtYuanbao then
			SendRecommendTips(sysarg, PopUpWndType.PayWindow, Lang.ScriptTips.COM0009);
		end
		return
	end

	Actor.onDivorce(sysarg)
	
	local mySex = Actor.getIntProperty(sysarg,PROP_ACTOR_SEX)
		
	local fmsg = Lang.ScriptTips.m00042
	local name1 = ""
	local name2 = ""
	if mySex == 0 then
		name1 = Actor.getName(sysarg)
		name2 = Actor.getName(another)
	else
		name1 = Actor.getName(another)
		name2 = Actor.getName(sysarg)
	end
	
	local tipmsg = string.format(fmsg, name1, name2)
	System.broadcastTipmsg(tipmsg, ttChatWindow)
	
	Actor.closeNPCDialog(sysarg)
end

-- 征婚
t.on_lonely_hearts = function(sysarg, mycfg)	
	--local nLevel = Actor.getIntProperty(sysarg,PROP_CREATURE_LEVEL)
	if not Actor.checkActorLevel(sysarg, HongNiangCfg.needLevel, 0) then
		Actor.sendTipmsg(sysarg,string.format("{color;FFff0000;等级不足%d级}",HongNiangCfg.needLevel),ttFlyTip)
		return
	end
		
	if Actor.getSocialCurNum(sysarg,7) > 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.m00022, ttFlyTip)
		return
	end
	
	local myMoney = Actor.getMoneyCount(sysarg, mycfg.moneyType)
	if myMoney < mycfg.money then
		Actor.sendTipmsg(sysarg,string.format("{color;FFff0000;金币不足%d万}", math.floor(mycfg.money/10000)),ttFlyTip)
		return
	end
	
	if Actor.changeMoney(sysarg, mycfg.moneyType, -mycfg.money, LogIds.LonelyHeartsConsume, "longly_hearts") == false then
		if mycfg.moneyType == mtYuanbao then
			SendRecommendTips(sysarg, PopUpWndType.PayWindow, Lang.ScriptTips.COM0009);
		end
		return
	end

	local fmsg = "{color;FFe6dfb9;%s:}本人想寻觅一佳人做伴侣,有意者请火速前来!{moveto;%d;[前往结婚]}"	
		
	local id = 5
	for i=1, #ChuansongPoint do
		if ChuansongPoint[i].ncpid == 78 then
			id = ChuansongPoint[i].id
			break
		end
	end

	local tipmsg = string.format(fmsg, Actor.getName(sysarg), id)
	System.broadcastTipmsg(tipmsg, ttChatWindow)
	
	Actor.closeNPCDialog(sysarg)
end

-- 发红包
t.on_red_package = function(sysarg, mycfg)	
	local myMoney = Actor.getMoneyCount(sysarg, mycfg.moneyType)
	if myMoney < mycfg.money then
		Actor.sendTipmsg(sysarg, "{color;FFff0000;很遗憾,您的元宝不足}", ttFlyTip)
		return
	end
	
	local nCount = 0
	
	--local playerList = LuaHelp.getSceneActorListById(Actor.getSceneId(sysarg))
	
	local pos = HongNiangCfg.hongNiangNPCPos
	local playerList = LuaHelp.getNearEntityListFromXY(Actor.getScenePtr(sysarg), pos[1], pos[2], 12, 0)
	if playerList ~= nil then
		nCount = table.getn(playerList)
	end
	
	if nCount <= 0 then
		Actor.sendTipmsg(sysarg, "附近没有玩家,发送结婚红包失败", ttFlyTip)
		return
	end
	
	if Actor.changeMoney(sysarg, mycfg.moneyType, -mycfg.money, LogIds.MarryRedPkgConsume, "red_package") == false then
		if mycfg.moneyType == mtYuanbao then
			SendRecommendTips(sysarg, PopUpWndType.PayWindow, Lang.ScriptTips.COM0009);
		end
		return
	end
	
	-------------
	local id = 5
	for i=1, #ChuansongPoint do
		if ChuansongPoint[i].ncpid == 78 then
			id = ChuansongPoint[i].id
			break
		end
	end
	
	local fmsg = "{color;FFe6dfb9;%s}在红娘NPC旁发放了{color;FFFFFF00%d元宝}庆祝新人!{moveto;%d;[前往庆祝]}"	

	local tipmsg = string.format(fmsg, Actor.getName(sysarg), mycfg.money, id)
	System.broadcastTipmsg(tipmsg, ttChatWindow)
	------------------

	local sActorname = Actor.getName(sysarg) --获得发钱的人的名字
	local sceneId = Actor.getSceneId(sysarg)
			
	if nCount > 0 then		
		local leftMoney = mycfg.money
		local randMoney = 0
		if mycfg.money >= nCount then
			randMoney = mycfg.money - nCount -- 保证每人至少有1块钱
		end
				
		for j = 1, #playerList do
			if leftMoney <= 0 then
				break
			end
				
			local player = playerList[j]
			if player ~= nil then				
				local awardmoney = 0
				if j < nCount then
					if randMoney > 0 then
						awardmoney = System.getRandomNumber(randMoney)
						randMoney = randMoney - awardmoney
					end
					
					awardmoney = awardmoney + 1 -- 加上自己的1块钱
					if awardmoney > leftMoney then
						awardmoney = leftMoney
						leftMoney = 0
						if awardmoney == 0 then
							break
						end
					else
						leftMoney = leftMoney - awardmoney
					end
				else
					awardmoney = leftMoney
					leftMoney = 0
					
					if awardmoney == 0 then
						break
					end
				end
								
				if awardmoney > 0 then
					--Actor.playScrSceneEffectCode(player,MarriedConfig.RedBagConfig[nIndex].effectId,MarriedConfig.RedBagConfig[nIndex].effectTime,Actor.getSceneId(player))				
					Actor.changeMoney(player,mycfg.moneyType,awardmoney,LogIds.GetMarryRedPkg,sActorname)

					local award_msg = string.format("{color;FFe6dfb9%s}获得了%d元宝", Actor.getName(player), awardmoney)
					t.NearBroadCast(playerList, award_msg)
				end
			end
		end
	end
	
	Actor.closeNPCDialog(sysarg)

end

-- 同屏广播
t.NearBroadCast = function(playerList, msg)
	for j = 1, #playerList do							
		local player = playerList[j]
		if player ~= nil then
			Actor.sendTipmsg(player, msg, ttFlyTip)
		end			
	end
end
